﻿package  com.devilcat.ws{
	import com.devilcat.ws.Basicws;
	import com.worlize.websocket.*;
	import com.devilcat.dt.Gamedt;
	import com.devilcat.dt.Pokemondt;
	import com.devilcat.ui.Battleui;
	import com.devilcat.ui.PVPBattleui;
	
	public class PVPLoadPokemonListws extends Basicws{
		var mUid:int;
		public function PVPLoadPokemonListws(uid:int) {
			super("iw-protocol");
			mUid=uid;
		}
		override function handleWebSocketOpen(event:WebSocketEvent):void
		{
			var pack:Object = new Object();
			pack.uid=mUid;
			pack.action="loaduserpokemon";
			var myJson:String = JSONString(pack);
			websocket.sendUTF(myJson);
		}
		override function handleWebSocketMessage(event:WebSocketEvent):void
		{
			if (event.message.type === WebSocketMessage.TYPE_UTF8)
			{	
				if(Gamedt.pvp){
					var i:int;
					var pklist:Array = new Array();				
					var pokemonList:Array = new Array();
					pklist =	JSONObject(event.message.utf8Data);
					for (i=0; i<pklist.length;i++){
						pokemonList[i]=new Pokemondt();
						pokemonList[i].setPersonal(pklist[i]);
						pokemonList[i].uid=mUid;
					}		
//					for (i=0; i<pokemonList.length; i++){
//						if(pokemonList[i].upid==leadpkm){
//							var temp:Pokemondt =pokemonList[i];
//							pokemonList[i]=pokemonList[0];
//							pokemonList[0]=temp;
//						}
//					}

					Gamedt.MainUI.BeforeBattle();
					var newBattleUI:PVPBattleui = new PVPBattleui(pokemonList,mUid);
					Gamedt.MainUI.BlankMC.addChild(newBattleUI);
					trace("PVP BATTLE");
				}
			}
			closeConnect();
		}

	}
	
}
